package com.aiyun.rabbit.rabbitdemo.config;

import java.util.HashMap;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Exchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class OrderQueueConfiguration {

	@Bean
	public DirectExchange orderExchange(){
		return new DirectExchange("exchange.order");
	}

	@Bean
	public Queue orderConfirm(){
		return new Queue("queue.order.confirm");
	}

	@Bean
	public Queue orderCheck(){
		return new Queue("queue.order.check");
	}

	@Bean
	public Queue deadLetterOrderCheck(){
		HashMap<String, Object> params = new HashMap<>();
		params.put("x-dead-letter-exchange", "exchange.order");
		params.put("x-dead-letter-routing-key", "check");
		params.put("x-message-ttl", 10000);
		return new Queue(
				"dead-letter-order-check",
				true,
				false,
				false,
				params
		);
	}

	@Bean
	public Binding bindingOrderConfirmToOrderExchange(){
		return BindingBuilder.bind(orderConfirm()).to(orderExchange()).with("confirm");
	}

	@Bean
	public Binding bindingOrderCheck(){
		return BindingBuilder.bind(orderCheck()).to(orderExchange()).with("check");
	}

	@Bean
	public Binding bindDeadLetterOrderCheck(){
		return BindingBuilder.bind(deadLetterOrderCheck()).to(orderExchange()).with("delay-order-check");
	}
}
